package com.jielihaofeng.springbootredis.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @description Redis工具类
 * @author Johnnie Wind
 * @date 2020/11/29 19:54
 */
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * @description 指定缓存失效时间
     * @author Johnnie Wind
     * @date 2020/11/29 19:57
     * @Param [key, time]
     * @return boolean
     */
    public boolean expire(String key,long time){
        try {
            if (time>0){
                redisTemplate.expire(key,time, TimeUnit.SECONDS);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 根据key获取过期时间
     * @author Johnnie Wind
     * @date 2020/11/29 19:58
     * @Param [key]
     * @return long
     */
    public long getExpire(String key){
        return redisTemplate.getExpire(key);
    }

    /**
     * @description 判断key是否存在
     * @author Johnnie Wind
     * @date 2020/11/29 19:59
     * @Param [key]
     * @return boolean
     */
    public boolean hasKey(String key){
        try{
            return redisTemplate.hasKey(key);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 删除缓存
     * @author Johnnie Wind
     * @date 2020/11/29 20:01
     * @Param [key]
     * @return void
     */
    public void del(String... key){
        if (key!=null&&key.length>0){
            if (key.length==1){
                redisTemplate.delete(key[0]);
            }else {
                redisTemplate.delete((Collection<String>) CollectionUtils.arrayToList(key));
            }
        }
    }

    /**
     * @description 根据key获取value
     * @author Johnnie Wind
     * @date 2020/11/29 20:02
     * @Param [key]
     * @return java.lang.Object
     */
    public Object get(String key){
        return key==null?null:redisTemplate.opsForValue().get(key);
    }

    /**
     * @description set 方法
     * @author Johnnie Wind
     * @date 2020/11/29 21:38
     * @Param [key, value]
     * @return boolean
     */
    public boolean set(String key,Object value){
        try{
            redisTemplate.opsForValue().set(key, value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * @description set 方法带时间
     * @author Johnnie Wind
     * @date 2020/11/29 21:37
     * @Param [key, value, time]
     * @return boolean
     */
    public boolean set(String key,Object value,long time){
        try{
            if (time>0){
                redisTemplate.opsForValue().set(key, value, time);
            }else {
                set(key, value);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    /**
     * @description 增加
     * @author Johnnie Wind
     * @date 2020/11/29 21:41
     * @Param [key, delta]
     * @return long
     */
    public long incr(String key,long delta){
        if (delta<0){
            throw new RuntimeException("递增数必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * @description 增加
     * @author Johnnie Wind
     * @date 2020/11/29 21:41
     * @Param [key, delta]
     * @return long
     */
    public long decr(String key,long delta){
        if (delta<0){
            throw new RuntimeException("递增数必须大于0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }
    
    /**
     * @description hash set
     * @author Johnnie Wind
     * @date 2020/11/29 21:44
     * @Param [key, item, value]
     * @return boolean
     */
    public boolean hSet(String key,String item,String value){
        try {
            redisTemplate.opsForHash().put(key,item,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description hash set 并设置时间
     * @author Johnnie Wind
     * @date 2020/11/29 21:44
     * @Param [key, item, value]
     * @return boolean
     */
    public boolean hSet(String key,String item,String value,long time){
        try {
            redisTemplate.opsForHash().put(key,item,value);
            if (time>0){
                expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description hash mset
     * @author Johnnie Wind
     * @date 2020/11/29 21:47
     * @Param [key, map]
     * @return boolean
     */
    public boolean hmset(String key, Map<String,Object> map){
        try{
            redisTemplate.opsForHash().putAll(key,map);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description Hash mSet并设置时间
     * @author Johnnie Wind
     * @date 2020/11/29 21:49
     * @Param [key, map, time]
     * @return boolean
     */
    public boolean hmset(String key,Map<String,Object> map,long time){
        try{
            redisTemplate.opsForHash().putAll(key,map);
            if (time>0){
                expire(key, time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description hash get
     * @author Johnnie Wind
     * @date 2020/11/29 21:51
     * @Param [key, item]
     * @return java.lang.Object
     */
    public Object hGet(String key,String item){
        return redisTemplate.opsForHash().get(key,item);
    }

    /**
     * @description 获取 hashkey 的所有键值
     * @author Johnnie Wind
     * @date 2020/11/29 21:52
     * @Param [key]
     * @return java.util.Map<java.lang.Object,java.lang.Object>
     */
    public Map<Object,Object> hmget(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * @description 删除 hash 表的数据
     * @author Johnnie Wind
     * @date 2020/11/29 21:53
     * @Param [key, item]
     * @return void
     */
    public void hDel(String key,Object... item){
        redisTemplate.opsForHash().delete(key,item);
    }

    /**
     * @description 判断hash表是否存在数据
     * @author Johnnie Wind
     * @date 2020/11/29 21:56
     * @Param [key, item]
     * @return boolean
     */
    public boolean hHasKey(String key,String item){
        return redisTemplate.opsForHash().hasKey(key,item);
    }

    /**
     * @description hash递增，如果不存在就会创建一个，返回递增后的值
     * @author Johnnie Wind
     * @date 2020/12/3 9:54
     * @Param [key, item]
     * @return boolean
     */
    public double hIncr(String key,String item,double by){
        return redisTemplate.opsForHash().increment(key,item,by);
    }

    /**
     * @description hash递减
     * @author Johnnie Wind
     * @date 2020/12/3 9:54
     * @Param [key, item]
     * @return boolean
     */
    public double hDecr(String key,String item,double by){
        return redisTemplate.opsForHash().increment(key,item,-by);
    }

    //===========================set=======================
    /**
     * @description 根据key获取set中的所有值
     * @author Johnnie Wind
     * @date 2020/12/3 9:58
     * @Param [key]
     * @return java.util.Set<java.lang.Object>
     */
    public Set<Object> sGet(String key){
        try{
            return redisTemplate.opsForSet().members(key);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description 根据value从一个set中查值
     * @author Johnnie Wind
     * @date 2020/12/3 10:00
     * @Param key
     * @param object
     * @return 
     */
    public boolean sHasKey(String key,Object object){
        try {
            return redisTemplate.opsForSet().isMember(key, object);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 将数据放入set
     * @author Johnnie Wind
     * @date 2020/12/3 10:05
     * @Param [key, values]
     * @return long
     */
   public long sSet(String key,Object... values){
        try{
            return redisTemplate.opsForSet().add(key,values);
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
   }

   /**
    * @description 将数据放入set，并设置过期时间
    * @author Johnnie Wind
    * @date 2020/12/3 10:07
    * @Param [key, time, values]
    * @return long
    */
    public long sSet(String key,long time,Object... values){
        try{
            Long count = redisTemplate.opsForSet().add(key, values);
            if (time>0){
                expire(key,time);
            }
            return count;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * @description 获取set的长度
     * @author Johnnie Wind
     * @date 2020/12/3 10:12
     * @Param [key]
     * @return long
     */
    public long sGetSetSzie(String key){
        try{
            return redisTemplate.opsForSet().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * @description 移除值为value的
     * @author Johnnie Wind
     * @date 2020/12/3 10:13
     * @Param [key, value]
     * @return long
     */
    public long sRemove(String key,Object... value){
        try{
            return redisTemplate.opsForSet().remove(key,value);
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    //===========================List=======================
    /**
     * 获取list的长度
     */
    public long lGetListSize(String key){
        try{
            return redisTemplate.opsForList().size(key);
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }

    /**
     * @description 通过索引获取List中的值
     * @author Johnnie Wind
     * @date 2020/12/3 10:20
     * @Param [key, index]
     * @return java.lang.Object
     */
    public Object lGetIndex(String key, long index){
        try{
            return redisTemplate.opsForList().index(key,index);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @description 存放数据到list
     * @author Johnnie Wind
     * @date 2020/12/3 10:29
     * @Param [key, value]
     * @return java.lang.Object
     */
    public Object lSet(String key, Object value){
        try{
            redisTemplate.opsForList().rightPush(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 存放数据到list,并设置时间
     * @author Johnnie Wind
     * @date 2020/12/3 10:37
     * @Param [key, value, time]
     * @return java.lang.Object
     */
    public Object lSet(String key, Object value,long time){
        try{
            redisTemplate.opsForList().rightPush(key,value);
            if (time>0){
                expire(key,time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 将list集合放入缓存
     * @author Johnnie Wind
     * @date 2020/12/3 10:33
     * @Param [key, value]
     * @return java.lang.Object
     */
    public Object lSet(String key, Object... value){
        try{
            redisTemplate.opsForList().rightPushAll(key,value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 将list集合放入缓存,并设置时间
     */
    public Object lSet(String key, long time,Object... value){
        try{
            redisTemplate.opsForList().rightPushAll(key,value);
            if (time>0){
                expire(key, time);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * @description 根据索引修改list中的某条数据
     * @author Johnnie Wind
     * @date 2020/12/3 10:35
     * @Param [key, index, value]
     * @return boolean
     */
    public boolean lUpdateIndex(String key ,long index ,Object value){
        try{
            redisTemplate.opsForList().set(key, index, value);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 移除n个值为value
     */
    public long lRemove(String key,long count,Object value){
        try{
            long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        }catch (Exception e){
            e.printStackTrace();
            return 0L;
        }
    }
}
